<template>
  <div class="sz-step-upload">
    <div class="tag" v-if="stepConfig.tagText">{{ stepConfig.tagText }}</div>
    <div class="top">
      <div class="top_icon">
        <slot name="icon"></slot>
        <i v-if="!$slots.icon" class="el-icon-upload"></i>
      </div>
      <div class="flex">
        <div class="top_text">
          <div class="title">
            <span>{{ stepConfig.title }}</span>
            <el-button
              v-if="stepConfig.downloadUrl"
              class="download"
              type="text"
              icon="el-icon-download"
              @click="handleDownload(stepConfig.downloadUrl)"
            >
              {{ stepConfig.downloadText }}
            </el-button>
          </div>
          <div class="subTitle">
            {{ stepConfig.subTitle }}
          </div>
        </div>
        <div class="top_action">
          <SzUpload
            ref="szUploadRef"
            :show-file-list="false"
            @getFileList="handleGetFileList"
            v-bind="$attrs"
            v-on="$listeners"
          >
            <slot name="action"></slot>
            <template v-if="!$slots.action">
              <el-button size="small" type="primary">{{ btnText }}</el-button>
            </template>
          </SzUpload>
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="tip" v-if="stepConfig.tip">
        <i class="el-icon-warning-outline"></i>
        {{ stepConfig.tip }}
      </div>
      <el-divider v-if="fileList && fileList.length" class="divider"></el-divider>
      <div class="fileList">
        <FileList :fileList="fileList" @remove="handleRemove"/>
      </div>
    </div>
  </div>
</template>

<script>
import SzUpload from "./index.vue";
import FileList from './fileList.vue';
export default {
  name: "SzStepUpload",
  inheritAttrs: false,
  components: { SzUpload, FileList },
  data() {
    return {
      fileList: []
    }
  },
  props: {
    stepConfig: {
      type: Object,
      default: () => ({
        tagText: "步骤",
        title: "该步骤标题",
        downloadText: "下载模板",
        downloadUrl: "111",
        subTitle: "步骤说明步骤说明步骤说明步骤说明步骤说明步骤说明步骤说明步骤说明步骤说明步骤说明",
        tip: "该步骤提示"
      })
    },
    btnText: {
      type: String,
      default: "点击上传"
    }
  },
  methods: {
    handleGetFileList(list) {
      this.fileList = list
    },
    // 清空上传列表
    handleClear() {
      this.fileList = [];
      if (this.$refs.szUploadRef) {
        this.$refs.szUploadRef.fileList = [];
      }
    },
    handleRemove(file) {
      this.fileList = this.fileList.filter((f) => f.uid !== file.uid);
      if (this.$refs.szUploadRef) {
        this.$refs.szUploadRef.handleRemove(file);
      }
    },
    handleDownload(url) {
      this.$message({
        showClose: true,
        message: "模板下载成功！",
        type: "success"
      });
      window.location.href = url;
    }
  }
};
</script>

<style lang="scss" scoped>
.sz-step-upload {
  position: relative;
  background-color: #fafafa;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 20px;
  .tag {
    background-color: #69B1FF;
    color: #fff;
    position: absolute;
    top: 0;
    left: 0;
    font-size: 12px;
    padding: 0 4px;
    border-radius: 4px 4px 4px 0;
    font-weight: 400;
    line-height: 20px;
  }
  .flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
  }
  .top {
    display: flex;
    align-items: center;

    &_icon {
      width: 38px;
      margin-right: 15px;
      i {
        font-size: 38px;
        color: #c0c4cc;
      }
    }

    &_text {
      .title {
        display: flex;
        align-items: center;
        > span {
          font-weight: 600;
          font-size: 16px;
          line-height: 24px;
        }
        .download {
          margin-left: 12px;
          padding: 0;
          ::v-deep span {
            margin-left: 0 !important;
          }
        }
      }
      .subTitle {
        color: #606266;
        line-height: 22px;
        font-weight: 400;
        font-size: 14px;
        word-break: break-all;
        margin: 8px 0;
      }
    }

    &_action {
      flex: 1;
      text-align: right;
      margin-left: 15px;
    }
  }
  .bottom {
    margin-left: 53px;
    .tip {
      color: #A8ABB2;
      font-size: 13px;
      font-weight: 400;
      margin-top: 4px;
    }
    .divider {
      color: #EBEEF5;
      margin-bottom: 16px;
    }
  }
}
</style>
